package controller;

/*
 * Action for researchers to input question for the questionnaire.
 * Used by both Binary and Network Binary type questions.
 */

import formbeans.LikertChoiceForm;
import formbeans.LikertForm;
import helper.EntityAttributeType;
import helper.UserType;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import databeans.Likertq;
import databeans.Question;
import databeans.User;
import model.LikertqDAO;
import model.Model;
import model.QuestionDAO;

public class LikertqAction extends Action {
	private FormBeanFactory<LikertForm> likertformBeanFactory = FormBeanFactory.getInstance(LikertForm.class);
	private FormBeanFactory<LikertChoiceForm> choiceformBeanFactory = FormBeanFactory.getInstance(LikertChoiceForm.class);
	private LikertqDAO likertqDAO;
	private QuestionDAO questionDAO;
	
	public LikertqAction(Model model) {
		likertqDAO = model.getLikertqDAO();
		questionDAO = model.getQuestionDAO();
	}
	
    public String getName() {
		return "likertq.do";
	}
    
    public String perform(HttpServletRequest request) {

        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
    	// First, validate whether researcher is accessing this
    	HttpSession session = request.getSession(false);
    	User user = (User) session.getAttribute("user");
    	
    	if (user.getType() != UserType.RESEARCHER) {
    		errors.add("You are not a researcher!");
    		return "homepage.jsp";
    	}
    	
    	// Validate the question number
    	Question question = (Question) session.getAttribute("question");
    	
    	if (question == null) {
    		errors.add("Invalid question number");
    		return "createsurvey.jsp";
    	}
    	
		String button = request.getParameter("button");
		
		if (button == null)
			return "likertq.jsp";
		
		if (button.equals("Back")) {
			synchronized(session) {
				session.setAttribute("question", null);
			}
			return "createsurvey.jsp";
		}

		if (button.equals("Submit") || button.equals("Next")) {
	        try {
	        	String position = (String) session.getAttribute("likert");
	        	
	        	if (position == null)
	        		return "createsurvey.jsp";
	        	
	        	if (position.equals("first")) {
	        		LikertForm form = likertformBeanFactory.create(request);
	            	request.setAttribute("likertqform",form);
	            	
	            	if (!form.isPresent()) {
	            		return "likertq.jsp";
	            	}
	            	
	        		// Validate for errors in the form
	        		errors.addAll(form.checkLikertFormErrors());
	        		if (errors.size() != 0) {
	        			return "likertq.jsp";
	        		}
	        		
	        		question.setQuestion(form.getQuestion());
	        		question.setDynetmlAttr(form.getDynetmlAttr());
	        		questionDAO.updateQuestion(question);
	        		synchronized(session) {
	        			session.setAttribute("question", question);
	        		}
	        		
	        		if (form.getType().equals("5"))
	        			session.setAttribute("likert", "five");
	        		else if (form.getType().equals("7"))
	        			session.setAttribute("likert", "seven");
	        		else {
	        			errors.add("Illegal select value of " + form.getType());
	        			return "errors.jsp";
	        		}
	        		LikertChoiceForm form1 = choiceformBeanFactory.create(request);
	            	request.setAttribute("likertqform",form1);
	        		return "likertq.jsp";
	        	}
	        	else {
	        		LikertChoiceForm form = choiceformBeanFactory.create(request);
	            	request.setAttribute("likertqform",form);
	            	
	            	if (!form.isPresent()) {
	            		return "likertq.jsp";
	            	}
	            	
	        		// Validate for errors in the form
	        		errors.addAll(form.checkLikertChoiceFormErrors(position));
	        		if (errors.size() != 0) {
	        			return "likertq.jsp";
	        		}

		        	try {
	            		Integer.parseInt(form.getChoice1());
	            		Integer.parseInt(form.getChoice2());
	            		Integer.parseInt(form.getChoice3());
	            		Integer.parseInt(form.getChoice4());
	            		Integer.parseInt(form.getChoice5());
	            		if (position.equals("seven")) {
	            			Integer.parseInt(form.getChoice6());
	            			Integer.parseInt(form.getChoice7());
	            		}
	            		question.setDynetmlType(EntityAttributeType.CATEGORY_NUMBER);
	        		} catch (NumberFormatException e) {
	        			question.setDynetmlType(EntityAttributeType.CATEGORY_TEXT);
	        		}
		        		
	        		questionDAO.updateQuestion(question);
	        		synchronized(session) {
	        			session.setAttribute("question", question);
	        		}
	        		
	        		Likertq likertq = new Likertq(question.getQid());
	        		likertq.setChoice1(form.getChoice1());
	        		likertq.setChoice2(form.getChoice2());
	        		likertq.setChoice3(form.getChoice3());
	        		likertq.setChoice4(form.getChoice4());
	        		likertq.setChoice5(form.getChoice5());
	        		if (position.equals("seven")) {
	        			likertq.setChoice6(form.getChoice6());
	        			likertq.setChoice7(form.getChoice7());
	        			likertq.setNumChoices(7);
	        			
	        			if (form.getType().equals("1,2,3,4,5,6,7"))
	        				likertq.setType(1);
	        			else
	        				likertq.setType(2);
	        		}
	        		else {
	        			likertq.setChoice6("");
	        			likertq.setChoice7("");
	        			likertq.setNumChoices(5);
	        			
	        			if (form.getType().equals("1,2,3,4,5"))
	        				likertq.setType(1);
	        			else
	        				likertq.setType(2);
	        		}
	        		likertqDAO.create(likertq, questionDAO);
	        		
	        		Question[] questionlist = questionDAO.getQuestionBySidSortedQnum(question.getSid(), true);
	    			synchronized(session) {
	    				session.setAttribute("questionlist", questionlist);
	    			}
	        		return "surveysummary.jsp";
	        	}
	        } catch (DAOException e) {
	        	errors.add(e.getMessage());
	        	return "errors.jsp";
	        } catch (FormBeanException e) {
	        	errors.add(e.getMessage());
	        	return "errors.jsp";
	        }
        }
        errors.add("Illegal button value of " + button);
		return "errors.jsp";
    }
}
